app.controller("permissionCtrl", function ($scope,$interval, $http, permissionService) {

    //----------分页参数------------
    $scope.pageRowSize = 10;
    $scope.curPage = 1;

    //-----------------------------

    $scope.object = {};
    $scope.formMode = false;
    $scope.errors = {};

    //--------------初始化菜单---------------
    $scope.initMenu = function () {
        showSecondMenu('billingManage');
        selectSecondMenu('permissionManage');
    }
    $scope.initMenu();

    //---------------错误信息--------------
    $scope.requestError = function () {
        $scope.errorMessage = "unknown error!"
    };

    $scope.init = function () {
        $scope.loadContractUsers();
    };

    //查询用户
    $scope.loadContractUsers = function () {
        $scope.pageRowSize = 10;
        $scope.curPage = 1;
        loadPage(findContractUsers, $scope, $scope.pageRowSize);
    };
    function findContractUsers(curPage,pageRow,flag) {
        $.fn.jqLoading({ height: 100, width: 240, text: "查询中，请稍等...." });
        var params = {action:"findContractUsers",pageNum:curPage,pageSize:pageRow};
        params["contractUserId"] = $("#contractUserSel").val();
        params["userType"] = $("#contractUserTypeSel").val();
        permissionService.findContractUsers(params, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status == 'SUCCESS'){
                $scope.pageList = data.data.voList;
                $scope.size = $scope.pageList.pages;
                $scope.contractUsers = $scope.pageList.list;
                if ($scope.size > 0) {
                    $('.page-container').show();
                    var page = {
                        curPage: $scope.pageList.pageNum,
                        totalPage: $scope.size,
                        rowCount: $scope.pageList.total
                    };
                    if(flag){
                        $('.page-container').setPage(page);
                    }else{
                        $('.page-container').refreshPage(page);
                    }
                } else {
                    $('.page-container').hide();
                }
            } else {
                promptMsg(data.errorReason);
            }
        });
    }

    //设置权限
    $scope.toEditPermission = function (contractUserId) {
        permissionService.getPermissionsTree({
            action: 'getPermissionsTree',
            contractUserId :  contractUserId
        }, function (data) {
            $scope.contractUserId = contractUserId;
            var treeObj = $.fn.zTree.init($("#editPermissionTree"), editPermissionSetting, data);
            //滚动条回到顶端
            $("#loadPermission").animate({scrollTop:0},200);
            //模态框居中： 将事件监听的事件改成show.bs.modal 即可解决
            $('#editPermissionModal').on('show.bs.modal', centerModals("editPermissionModal"));
            $("#editPermissionModal").modal({
                backdrop: "static",//点击空白处不关闭对话框
                keyboard:false // 按esc键盘不关闭
            });
        }, $scope.requestError());
    }
    //设置权限Tree设置
    var editPermissionSetting = {
        check: {
            enable: true,//显示复选框
            chkboxType: { "Y":"ps", "N":"ps"}
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    }
    //编辑权限
    $scope.editPermissions = function () {
        var treeObj = $.fn.zTree.getZTreeObj("editPermissionTree"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        $scope.permissionIds = v;
        var obj;
        for(var i = 0;i < nodes.length;i++){
            obj = nodes[i];
            //如果不是父节点
            if(!obj.isParent){
                if(v != ""){
                    v += ",";
                }
                v += obj.id;
                $scope.permissionIds = v;
            }
        }

        //关联用户
        permissionService.editPermissions({
            action : 'editPermissions',
            contractUserId :  $scope.contractUserId,
            permissionIds :  $scope.permissionIds
        }, function (data) {
            if (data.status == "SUCCESS") { //编辑成功，关闭界面，刷新列表
                promptMsg(data.data.msg);
                //关闭设置权限窗口
                $scope.closeEditPermissionModal();
            }else {
                promptMsg(data.errorReason);
            }
        }, $scope.requestError());
    }
    //关闭设置权限窗口
    $scope.closeEditPermissionModal = function(){
        $("#editPermissionModal").modal("hide");
    };

    //通过类型获取合同用户
    $scope.getContractUsersByUserType = function(){
        var params = {action:"getContractUsersByUserType"};
        params["userType"] = $("#contractUserTypeSel").val();
        permissionService.getContractUsersByUserType(params, function (data) {
            if (data.status == 'SUCCESS'){
                $("#contractUserSel").empty();
                var contractUserList = data.data.contractUserList;
                $("#contractUserSel").append("<option value=''><--请选择--></option>");
                for (var i = 0; i < contractUserList.length; i++) {
                    $("#contractUserSel").append("<option value='" + contractUserList[i].contractUserId + "'>" + contractUserList[i].userAbbrev + "</option>");
                }
            } else {
                promptMsg(data.errorReason);
            }
        });

        //重新加载合同用户
        $scope.loadContractUsers();
    };
});